package com.hmdp.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;

@Component
public class RedisWork {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    //开始时间戳
    public static final long BEGIN_TIME = 1735689600;

    public long getId(String keyPrefix){
        //生成时间戳
        long time = LocalDateTime.now().toEpochSecond(ZoneOffset.UTC) - BEGIN_TIME;

        //生成序列号
        String date = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy:MM:dd"));
        String key = "icr:" + keyPrefix + ":" + date;
        Long increment = stringRedisTemplate.opsForValue().increment(key);

        return time<<32 | increment;


    }
}
